import React from 'react'

import moment from "moment";
import * as utils from '../common/utils.js'
import Taro from "@tarojs/taro"

import TabBar from "../common/taBar"
import { APIBASEURL } from '../constants/global.js'
import { View, Text, Block } from '@tarojs/components'
import './index.less'
/**
 * 我的挂号 主页
 */
class Index extends React.Component { 

  state = {
    registrations: [],
    week: '',
    day: ''
  }

  getDayAndWeek(registerDate) {
    let formatDate = utils.timeFormatSeconds(registerDate);
    this.setState({
      week: utils.getWeekday(formatDate),
      day: utils.getHalfDay(formatDate)
    })
  }
  componentDidShow() {
    console.log("进来-------------")
    Taro.getStorage({
      key: '__itemcode__',
      success: (res) => {
        const itemcode = res.data;
        console.log("itemcode:" + itemcode)
        Taro.request({
          url: `${APIBASEURL}/myRegisters?page=1&size=3&itemcode=${itemcode}`,
          header: {
            'content-type': 'application/json' // 默认值
          },
          method: 'GET',
          dataType: 'json',
          credentials: 'include',
          success: (res) => {
            this.setState({
              registrations: res.data.data
            })
            console.log("<>>", res.data)
            console.log("<>>", this.state.registrations)
            if (this.state.registrations && this.state.registrations.length == 0) {
              Taro.showToast({
                title: '您没有挂号记录！',
                icon: 'none',
                duration: 3000
              })
            }
          },
          fail: function (errMsg) {
            Taro.showToast({
              title: '服务器请求失败!',
              icon: 'none',
              duration: 3000
            })
          }
        });
      }
    })

  }

  render() {

    let registrations = this.state.registrations;
    return (
      <View className='index-registration'>
        {
          registrations.map((registration, index) => {
            let classNames = {};
            switch (registration.registerType) {
              case '1':
                classNames = {
                  stateClass: 'tip-card',
                  numberClass: 'tip-number'
                }
                break;
              case '0':
                classNames = {
                  stateClass: 'tip-card',
                  numberClass: 'tip-number'
                }
                break;
              case '2':
                classNames = {
                  stateClass: 'tip-card',
                  numberClass: 'tip-number'
                }
                break;
            }
            // className={classNames}
            // return <Registration   />;
            return <Block>
              <view className='card-one'>
                <view className='info-list'>
                  <view className='text-two'>
                    <Text>就诊人:</Text>
                    <Text>{registration.patientName}</Text>
                  </view>
                  <view className='text-two'>
                    <Text>就诊机构:</Text>
                    <Text>{registration.hospitalName}</Text>
                  </view>
                  <view className='text-two'>
                    <Text>就诊科室:</Text>
                    <Text>{registration.deptCode}</Text>
                  </view>
                  <view className='text-two'>
                    <Text>就诊医师:</Text>
                    <Text>{registration.doctorName}</Text>
                  </view>
                  <view className='text-two'>
                    <Text>预约时间:</Text>
                    <Text>
                      {this.getDayAndWeek(registration.registerDate)}
                      {moment(registration.registerDate).format('YYYY-MM-DD')}&nbsp;&nbsp;
                       {this.state.week}&nbsp;&nbsp;
                       {this.state.day}
                    </Text>
                  </view>
                  <view className='tip-card'>
                    <Text>专家号</Text>
                    <Text></Text>
                  </view>
                </view>


                <view className='tip-number'>
                  <Text>{registration.registerCount}</Text>
                </view>
              </view>
            </Block>
          }
          )
        }
        <TabBar tabBarCurrent={3} />
      </View>
    )
  }
}

export default Index;
